using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using VintaSoft.Twain;

namespace ScanStation {
	/// <summary>
	/// Summary description for DevCapsForm.
	/// </summary>
	public class DevCapsForm :System.Windows.Forms.Form {
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button BGetCaps;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.Button BExit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public DevCapsForm() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing) {
			if(disposing) {
				if(components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.BGetCaps = new System.Windows.Forms.Button();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.BExit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
									| System.Windows.Forms.AnchorStyles.Left)
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox1.Location = new System.Drawing.Point(10, 4);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox1.Size = new System.Drawing.Size(386, 360);
			this.textBox1.TabIndex = 0;
			// 
			// BGetCaps
			// 
			this.BGetCaps.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.BGetCaps.Location = new System.Drawing.Point(12, 415);
			this.BGetCaps.Name = "BGetCaps";
			this.BGetCaps.Size = new System.Drawing.Size(189, 26);
			this.BGetCaps.TabIndex = 1;
			this.BGetCaps.Text = "Get capabilities";
			this.BGetCaps.Click += new System.EventHandler(this.BGetCaps_Click);
			// 
			// progressBar1
			// 
			this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
									| System.Windows.Forms.AnchorStyles.Right)));
			this.progressBar1.Location = new System.Drawing.Point(3, 378);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(393, 27);
			this.progressBar1.TabIndex = 2;
			// 
			// BExit
			// 
			this.BExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.BExit.Location = new System.Drawing.Point(207, 415);
			this.BExit.Name = "BExit";
			this.BExit.Size = new System.Drawing.Size(194, 26);
			this.BExit.TabIndex = 3;
			this.BExit.Text = "Exit";
			this.BExit.Click += new System.EventHandler(this.BExit_Click);
			// 
			// DevCapsForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(405, 450);
			this.Controls.Add(this.BExit);
			this.Controls.Add(this.progressBar1);
			this.Controls.Add(this.BGetCaps);
			this.Controls.Add(this.textBox1);
			this.Name = "DevCapsForm";
			this.Text = "Device capabilities";
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		private void BGetCaps_Click(object sender,System.EventArgs e) {
			MainForm mainForm;
			mainForm = (Owner as MainForm);

			textBox1.Text = "==========================================" + Environment.NewLine + Environment.NewLine;
			textBox1.Text += "Device name: " + mainForm.VSTwain1.GetSourceProductName(mainForm.VSTwain1.sourceIndex) + Environment.NewLine + Environment.NewLine;

			if(!mainForm.VSTwain1.OpenDataSource()) {
				MessageBox.Show("Can't open Data Source: " + mainForm.VSTwain1.errorString);
				return;
			}

			int i;
			// Units of measure
			UnitOfMeasure currUnitOfMeasure = mainForm.VSTwain1.unitOfMeasure;
			if(mainForm.VSTwain1.errorCode != 0)
				MessageBox.Show(mainForm.VSTwain1.errorString);
			else
				textBox1.Text += "Current unit of measure: " + currUnitOfMeasure.ToString() + Environment.NewLine;

			UnitOfMeasure[] arrUnitsOfMeasure = mainForm.VSTwain1.GetUnitsOfMeasure();
			string s = "Supported units of measure: ";
			for(i = 0; i < arrUnitsOfMeasure.Length; i++) {
				s = s + arrUnitsOfMeasure[i].ToString() + " ";
			}
			textBox1.Text += s + Environment.NewLine + Environment.NewLine;

			// Pixel types
			PixelType currPixelType = mainForm.VSTwain1.pixelType;
			if(mainForm.VSTwain1.errorCode != 0)
				MessageBox.Show(mainForm.VSTwain1.errorString);
			else
				textBox1.Text += "Current pixel type: " + currPixelType.ToString() + Environment.NewLine;

			PixelType[] arrPixelTypes = mainForm.VSTwain1.GetPixelTypes();
			s = "Supported pixel types: ";
			for(i = 0; i < arrPixelTypes.Length; i++) {
				s = s + arrPixelTypes[i].ToString() + " ";
			}
			textBox1.Text += s + Environment.NewLine + Environment.NewLine;

			// Resolutions
			int currRes = mainForm.VSTwain1.resolution;
			textBox1.Text += "Current resolution: " + currRes.ToString() + " dpi" + Environment.NewLine;

			int[] arrRes = mainForm.VSTwain1.GetResolutions();
			s = "Supported resolutions: ";
			if(mainForm.VSTwain1.capType == CapType.Range)
				s = s + "min = " + mainForm.VSTwain1.capMinValue.ToString() + " dpi, max = " + mainForm.VSTwain1.capMaxValue.ToString() + " dpi, step size = " + mainForm.VSTwain1.capStepSize.ToString() + " dpi";
			else
				for(i = 0; i < arrRes.Length; i++)
					s = s + arrRes[i].ToString() + " ";
			textBox1.Text += s + Environment.NewLine + Environment.NewLine;

			// Page sizes
			PageSize currPSize = mainForm.VSTwain1.pageSize;
			textBox1.Text += "Current page size: " + currPSize.ToString() + Environment.NewLine;

			PageSize[] arrPSizes = mainForm.VSTwain1.GetPageSizes();
			if(mainForm.VSTwain1.errorCode == 0) {
				s = "Supported page sizes: ";
				for(i = 0; i < arrPSizes.Length; i++) {
					s = s + arrPSizes[i].ToString() + " ";
				}
				textBox1.Text += s + Environment.NewLine + Environment.NewLine;
			} else
				textBox1.Text += Environment.NewLine;

			textBox1.Text += "==========================================" + Environment.NewLine + Environment.NewLine;

			// Check for all device capabilities
			textBox1.Text += "Device capabilities:" + Environment.NewLine + Environment.NewLine;
			int j,k;

			mainForm.VSTwain1.capability = (int)VintaSoft.Twain.Capability.SupportedCaps;
			mainForm.VSTwain1.capType = VintaSoft.Twain.CapType.Enum;
			if(mainForm.VSTwain1.GetCap()) {
				if(mainForm.VSTwain1.capNumItems > 0) {
					progressBar1.Maximum = mainForm.VSTwain1.capNumItems;
					Capability[] caps = new Capability[mainForm.VSTwain1.capNumItems];
					Capability cap;
					float[] arr = mainForm.VSTwain1.capItems;
					for(k = 0; k < arr.Length; k++)
						caps[k] = (Capability)Convert.ToInt32(arr[k]);
					Array.Sort(caps);

					for(i = 0; i < caps.Length; i++) {
						cap = caps[i];
						mainForm.VSTwain1.capability = (int)cap;
						if(mainForm.VSTwain1.IsCapSupported()) {
							for(j = 1; j <= 4; j++) {
								mainForm.VSTwain1.capType = (VintaSoft.Twain.CapType)j;
								mainForm.VSTwain1.GetCap();

								if((int)mainForm.VSTwain1.capType == j) {
									switch(mainForm.VSTwain1.capType) {
										case VintaSoft.Twain.CapType.Array:
										case VintaSoft.Twain.CapType.Enum:
											s = cap.ToString();
											if(mainForm.VSTwain1.capType == VintaSoft.Twain.CapType.Array) s = s + " A ";
											else s = s + " E ";
											s = s + mainForm.VSTwain1.capValue.ToString() + " " + mainForm.VSTwain1.capNumItems.ToString() + " ";
											arr = mainForm.VSTwain1.capItems;
											for(k = 0; k < arr.Length; k++)
												s = s + arr[k].ToString() + " ";
											break;

										case VintaSoft.Twain.CapType.OneValue:
											s = cap.ToString() + " O " + mainForm.VSTwain1.capValue.ToString() + " '" + mainForm.VSTwain1.capStringValue + "'";
											break;

										case VintaSoft.Twain.CapType.Range:
											s = cap.ToString() + " R " + mainForm.VSTwain1.capValue.ToString() + " " + mainForm.VSTwain1.capDefValue.ToString() + " " + mainForm.VSTwain1.capMinValue.ToString() + " " + mainForm.VSTwain1.capMaxValue.ToString() + " " + mainForm.VSTwain1.capStepSize.ToString();
											break;
									}
									textBox1.Text += s + Environment.NewLine;
								}
							}
							textBox1.Text += Environment.NewLine;
						}
						progressBar1.Value = i;
					}
				}
			}

			textBox1.Text += "==========================================" + Environment.NewLine + Environment.NewLine;

			VSTwain VSTwain1 = mainForm.VSTwain1;
			VSTwain1.capability = (int)Capability.SupportedCaps;
			VSTwain1.capType = CapType.Enum;
			if(VSTwain1.GetCap())
				if(VSTwain1.capNumItems > 0) {
					int[] caps = new int[VSTwain1.capNumItems]; int cap;
					for(int ci = 0; ci < VSTwain1.capNumItems; ci++)
						caps.SetValue(Convert.ToInt32(VSTwain1.capItems[ci]),ci);

					Array.Sort(caps);

					for(int c = 0; c < caps.Length; c++)
						if(caps[c] >= 32768)
							textBox1.Text += "Custom capability " + caps[c].ToString() + " is supported by device !"+Environment.NewLine;

				} else
					MessageBox.Show(VSTwain1.errorString);


			mainForm.VSTwain1.CloseDataSource();
		}

		private void BExit_Click(object sender,System.EventArgs e) {
			Close();
		}
	}
}
